﻿Public Class frmDMDoiTuong
    Private pPanel1Height As Integer
    Private pPanel2Height As Integer
    Private pPanel3Height As Integer

    Public selectedDT As CDoiTuong = Nothing
    Public bCallForm As Boolean = False 'Cho biet la form nay dc goi tu form khac
    Public eDoiTuong As enumDOITUONG = enumDOITUONG.NONE
    Public sFind As String = ""
    Public eThongTin As enumLabelThongTin = enumLabelThongTin.Find
    Public isInsert As Boolean = False 'Bien dung de xac dinh la Insert hay Update

    Public Enum enumLabelThongTin
        View = 0
        Find = 1
        Insert = 2
        Update = 3
    End Enum

    Public Sub ShowPanel(ByVal bPanel2 As Boolean, ByVal bPanel3 As Boolean)
        Panel2.Visible = bPanel2
        Splitter1.Visible = bPanel2
        Panel3.Visible = bPanel3
        Splitter2.Visible = bPanel3
        If bPanel2 And (Not bPanel3) Then
            Panel1.Height = pPanel1Height
            Panel2.Height = pPanel2Height + pPanel3Height
        ElseIf bPanel2 And bPanel3 Then
            Panel1.Height = pPanel1Height
            Panel2.Height = pPanel2Height
        ElseIf (Not bPanel2) And bPanel3 Then
            Panel1.Height = pPanel1Height + pPanel2Height
        ElseIf (Not bPanel2) And (Not bPanel3) Then
            Panel1.Height = pPanel1Height + pPanel2Height + pPanel3Height
        End If
    End Sub

    Public Sub Find(ByVal value As String)
        Dim i As Integer = lbxDoiTuong.FindStringExact(value)
        If (i <> -1) Then
            lbxDoiTuong.SelectedIndex = i
            lbxDoiTuong.Focus()
            lbxFind.DataSource = Nothing
            ShowPanel(False, False)
        ElseIf value <> "" Then
            Dim strSql As String = "select * from tblDMKhac where TEN like N'" + value + "%' and L = '" + LocDoiTuong(eDoiTuong) + "'"
            Dim arrFind As ArrayList = CDoiTuongs.GetDoiTuongListBySql(strSql)
            If arrFind.Count > 0 Then
                lbxFind.DataSource = arrFind
                lbxFind.DisplayMember = "TEN"
                lbxFind.ValueMember = "KH"
                ShowPanel(True, Panel3.Visible)
                lbxFind.Focus()
            Else
                ShowPanel(False, Panel3.Visible)
            End If
        End If
    End Sub

    Public Sub LoadDMDoiTuong(Optional ByVal strFind As String = "")
        Dim arrDonvi As ArrayList = CDoiTuongs.GetDoiTuongList(eDoiTuong)
        lbxDoiTuong.DataSource = arrDonvi
        lbxDoiTuong.DisplayMember = "TEN"
        lbxDoiTuong.ValueMember = "KH"
        lbxDoiTuong.Focus()
        If strFind <> "" Then
            Find(strFind)
        Else 'Neu ko co text can tim thi hien thi toan man hinh
            Panel1.Height = pPanel1Height + pPanel2Height + pPanel3Height
        End If
    End Sub

    Private Sub frmDMDoiTuong_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pPanel1Height = Me.Panel1.Height
        pPanel2Height = Me.Panel2.Height
        pPanel3Height = Me.Panel3.Height

        Panel2.Visible = False
        Splitter1.Visible = False
        Panel3.Visible = False
        Splitter2.Visible = False
        LoadDMDoiTuong(sFind)
    End Sub

    'To mau cho control khi Focus
    Private Sub textBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles txtTen.Enter, txtMa.Enter
        SetColorAllControls(sender, True)
    End Sub

    Private Sub textBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles txtTen.Leave, txtMa.Leave
        SetColorAllControls(sender, False)
    End Sub

    Private Sub lbxFind_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbxFind.SelectedIndexChanged
        If lbxFind.DataSource IsNot Nothing Then
            Dim objDonVi As CDoiTuong = lbxFind.SelectedItem
            Dim i As Integer = lbxDoiTuong.FindStringExact(objDonVi.Ten)
            If (i <> -1) Then
                lbxDoiTuong.SelectedIndex = i
            End If
        End If
    End Sub

    Public Function GetLabelThongTin(ByVal lblTT As enumLabelThongTin) As String
        Dim str As String = ""
        If lblTT = enumLabelThongTin.View Then
            str = "F1: Thêm - F2: Sửa - Delete: Xóa - F7:Tìm"
        ElseIf lblTT = enumLabelThongTin.Find Then
            str = "F1:Thêm - F2:Sửa - Delete:Xóa - F7:Tìm - Ctrl+F7:Đóng"
        ElseIf lblTT = enumLabelThongTin.Insert Then
            str = "F3: Thêm mới  -  F1: Đóng"
        Else
            str = "F3: Cập nhật  -  F2: Đóng"
        End If
        Return str
    End Function

    Private Sub lbx_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxDoiTuong.Enter, lbxFind.Enter
        If Panel2.Visible Then
            eThongTin = enumLabelThongTin.Find
        Else
            eThongTin = enumLabelThongTin.View
        End If
        lblThongTin.Text = GetLabelThongTin(eThongTin)
    End Sub

    Private Sub txt_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTen.Enter, txtMa.Enter, chkNhom.Enter
        If isInsert Then
            eThongTin = enumLabelThongTin.Insert
        Else
            eThongTin = enumLabelThongTin.Update
        End If
        lblThongTin.Text = GetLabelThongTin(eThongTin)
    End Sub

    Private Sub lbx_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbxFind.DoubleClick, lbxDoiTuong.DoubleClick
        If bCallForm Then
            selectedDT = CType(lbxDoiTuong.SelectedItem, CDoiTuong)
            If selectedDT.NH <> "" Then
                ShowMessages("Không chọn được đối tượng nhóm.")
                selectedDT = Nothing
                Exit Sub
            End If
            Me.Close()
        End If
    End Sub

    Public Function IsDoiTuongNhom(ByVal enumValue As enumDOITUONG) As Boolean
        If enumValue = enumDOITUONG.CCL _
        Or enumValue = enumDOITUONG.DVT _
        Or enumValue = enumDOITUONG.KHO _
        Or enumValue = enumDOITUONG.LOAIKHO _
        Or enumValue = enumDOITUONG.THANHTOAN Then
            Return False
        End If
        Return True
    End Function

    Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTen.KeyDown, txtMa.KeyDown, chkNhom.KeyDown
        'Kiem tra xem luc nhan F1 thi form co dang o trang thai Update ko: Neu ko co thi thuc hien Insert
        If e.KeyCode = Keys.F1 And eThongTin <> enumLabelThongTin.Update Then
            isInsert = True
            chkNhom.Enabled = False
            'Kiem tra xe Panel3 da visble chua: Neu chua thi tat invisible di
            If Panel3.Visible Then
                eThongTin = enumLabelThongTin.View
                ShowPanel(Panel2.Visible, False)
                lbxDoiTuong.Focus()
            Else
                eThongTin = enumLabelThongTin.Insert
                'Visible Panel3
                ShowPanel(Panel2.Visible, True)
                txtTen.Text = ""
                txtTen.Focus()
                'Lay doi tuong hien hanh
                Dim objDT As CDoiTuong = CType(lbxDoiTuong.SelectedItem, CDoiTuong)
                'Lay ma doi tuong moi
                txtMa.Text = CDoiTuongs.GetNewDoiTuong_ID(objDT.L, objDT.KH)
                'Nếu mã đối tượng mới là nhóm(length=3) thì check
                If txtMa.Text.Length = 3 Then
                    chkNhom.Checked = True
                Else
                    chkNhom.Checked = False
                End If
                txtMa.ReadOnly = False
            End If
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.F2 And eThongTin <> enumLabelThongTin.Insert Then 'Kiểm tra xem lúc nhấn F2 có đang ở trạng thái Update ko
            isInsert = False
            'Kiem tra xe Panel3 da visble chua: Neu chua thi tat invisible di
            If Panel3.Visible Then
                eThongTin = enumLabelThongTin.View
                ShowPanel(Panel2.Visible, False)
                lbxDoiTuong.Focus()
            Else
                eThongTin = enumLabelThongTin.Update
                ShowPanel(Panel2.Visible, True)
                txtTen.Focus()
                Dim objDT As CDoiTuong = CType(lbxDoiTuong.SelectedItem, CDoiTuong)
                txtTen.Text = objDT.Ten
                txtMa.Text = objDT.KH
                txtMa.ReadOnly = True
                If objDT.NH = "**" Then
                    chkNhom.Checked = True
                Else
                    chkNhom.Checked = False
                End If
                chkNhom.Enabled = True
                'Neu doi tuong nay co chua doi tuong con thi ko cho thay doi Nhom hay ko Nhom
                If (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblDPNX where LK = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblTON where LK = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.KHO) And BaseDB.ExecSql_DataReader("select * from tblTON where KHO  = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.DONVI) And BaseDB.ExecSql_DataReader("select * from tblDPNX where DVGN  = '" + objDT.KH + "'").Read()) Or _
                    (objDT.IsChildExists() Or objDT.KH.Length = 6) Then
                    chkNhom.Enabled = False
                End If
            End If
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.F3 Then
            Dim len As Integer = 0
            Dim objDT As New CDoiTuong
            objDT.L = LocDoiTuong(eDoiTuong)
            objDT.KH = txtMa.Text

            objDT.Ten = txtTen.Text
            len = objDT.ColumnLength("ten")
            If len < objDT.Ten.Length Then
                ShowMessages("Chiều dài ký tự của Tên đối tượng vượt quá " + len.ToString())
                txtTen.Focus()
                Exit Sub
            End If

            If chkNhom.Checked = True Then
                objDT.NH = "**"
            Else
                objDT.NH = Nothing
            End If

            If eThongTin = enumLabelThongTin.Insert Then
                If objDT.IsExists() Then
                    ShowMessages("Mã này đã tồn tại.")
                    Exit Sub
                End If
                If (objDT.KH.Length <> 3 And objDT.NH = "**") Then
                    ShowMessages("Mã nhóm phải có độ dài = 3.")
                    Exit Sub
                ElseIf objDT.NH <> "**" Then
                    If IsDoiTuongNhom(eDoiTuong) Then
                        If objDT.KH.Length <> 6 Then
                            ShowMessages("Mã chi tiết phải có độ dài = 6.")
                            Exit Sub
                        End If
                        'Kiem tra xem Ma cha co ton tai hay ko
                        Dim parentObj As New CDoiTuong
                        parentObj.L = objDT.L
                        parentObj.KH = objDT.KH.Substring(0, 3)
                        If Not parentObj.IsExists() Then
                            ShowMessages("Mã cha không tồn tại.")
                            Exit Sub
                        End If
                    End If
                End If
                objDT.Insert()
            Else
                objDT.Update()
            End If
            ShowPanel(False, False)
            LoadDMDoiTuong(objDT.Ten)
        End If
    End Sub

    Private Sub lbx_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lbxFind.KeyDown, lbxDoiTuong.KeyDown
        Dim objDT As CDoiTuong = lbxDoiTuong.SelectedItem
        If e.KeyCode = Keys.F1 And eThongTin <> enumLabelThongTin.Update Then
            isInsert = True
            chkNhom.Enabled = False
            If Panel3.Visible Then
                eThongTin = enumLabelThongTin.View
                ShowPanel(Panel2.Visible, False)
                lbxDoiTuong.Focus()
            Else
                eThongTin = enumLabelThongTin.Insert
                ShowPanel(Panel2.Visible, True)
                txtTen.Focus()
                txtTen.Text = ""
                txtMa.Text = CDoiTuongs.GetNewDoiTuong_ID(objDT.L, objDT.KH)
                If txtMa.Text.Length = 3 Then
                    chkNhom.Checked = True
                Else
                    chkNhom.Checked = False
                End If
                txtMa.ReadOnly = False
            End If
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.F2 And eThongTin <> enumLabelThongTin.Insert Then
            isInsert = False
            If Panel3.Visible Then
                eThongTin = enumLabelThongTin.View
                ShowPanel(Panel2.Visible, False)
                lbxDoiTuong.Focus()
            Else
                eThongTin = enumLabelThongTin.Update
                ShowPanel(Panel2.Visible, True)
                txtTen.Focus()
                txtTen.Text = objDT.Ten
                txtMa.Text = objDT.KH
                txtMa.ReadOnly = True
                If objDT.NH = "**" Then
                    chkNhom.Checked = True
                Else
                    chkNhom.Checked = False
                End If
                chkNhom.Enabled = True
                'Neu doi tuong nay co chua doi tuong con thi ko cho thay doi Nhom hay ko Nhom
                If (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblDPNX where LK = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblTON where LK = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.KHO) And BaseDB.ExecSql_DataReader("select * from tblTON where KHO  = '" + objDT.KH + "'").Read()) Or _
                   (objDT.L = LocDoiTuong(enumDOITUONG.DONVI) And BaseDB.ExecSql_DataReader("select * from tblDPNX where DVGN  = '" + objDT.KH + "'").Read()) Or _
                   (objDT.IsChildExists() Or objDT.KH.Length = 6) Then
                    chkNhom.Enabled = False
                End If
            End If
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.F7 AndAlso e.Control Then
            ShowPanel(False, Panel3.Visible)
            lbxDoiTuong.Focus()
            eThongTin = enumLabelThongTin.View
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.F7 Then
            Dim frm As New frmTimKiem
            frm.ShowDialog()
            sFind = frm.txtGiaTri.Text
            Find(sFind)
            eThongTin = enumLabelThongTin.Find
            lblThongTin.Text = GetLabelThongTin(eThongTin)
        ElseIf e.KeyCode = Keys.Delete Then
            'Kiem tra xem doi tuong nay co chua nhom con nao khong
            Dim str As String
            str = "select * from tblDMKHAC where left(KH,3) = '" + objDT.KH + "' and l = '" + objDT.L + "' and len(kh) > 3"
            If BaseDB.ExecSql_DataReader(str).Read() Then
                ShowMessages("Đối tượng này đang chứa đối tượng con nên không thể xóa.")
                Exit Sub
            ElseIf (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) Or objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT)) And _
                   CDoiTuongs.KiemTraTCNX(enumTCNX.DCSS, objDT.KH) Then
                ShowMessages("Tính chất này dùng để Điều chỉnh số lượng sổ sách nên không thể xóa.")
                Exit Sub
            ElseIf (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) Or objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT)) And _
            CDoiTuongs.KiemTraTCNX(enumTCNX.NB, objDT.KH) Then
                ShowMessages("Tính chất này dùng để Điều chỉnh nội bộ nên không thể xóa.")
                Exit Sub
            ElseIf (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) Or objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT)) And _
               CDoiTuongs.KiemTraTCNX(enumTCNX.DCT, objDT.KH) Then
                ShowMessages("Tính chất này dùng để Điều chỉnh tên vật tư nên không thể xóa.")
                Exit Sub
            ElseIf (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATNHAP) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
               (objDT.L = LocDoiTuong(enumDOITUONG.TINHCHATXUAT) And BaseDB.ExecSql_DataReader("select * from tblDPNX where TCNX = '" + objDT.KH + "'").Read()) Or _
               (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblDPNX where LK = '" + objDT.KH + "'").Read()) Or _
               (objDT.L = LocDoiTuong(enumDOITUONG.LOAIKHO) And BaseDB.ExecSql_DataReader("select * from tblTON where LK = '" + objDT.KH + "'").Read()) Or _
               (objDT.L = LocDoiTuong(enumDOITUONG.KHO) And BaseDB.ExecSql_DataReader("select * from tblTON where KHO  = '" + objDT.KH + "'").Read()) Or _
               (objDT.L = LocDoiTuong(enumDOITUONG.DONVI) And BaseDB.ExecSql_DataReader("select * from tblDPNX where DVGN  = '" + objDT.KH + "'").Read()) Then
                ShowMessages("Đối tượng này đã được chọn trong phiếu nhập xuất nên không thể xóa.")
                Exit Sub
            End If
            str = String.Format("Bạn muốn xóa ""{0}"" ra khỏi danh mục", objDT.Ten)
            If MessageBox.Show(str, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                objDT.Delete()
                ShowPanel(False, False)
                LoadDMDoiTuong()
            End If
        ElseIf e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Escape Then
            lbx_DoubleClick(Nothing, Nothing)
            Me.Close()
        End If
    End Sub

    Private Sub lbxDoiTuong_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbxDoiTuong.DrawItem
        Try
            If e.Index < 0 Then
                Exit Sub
            End If
            Dim brText As Brush
            Dim objDT As CDoiTuong = Me.lbxDoiTuong.Items(e.Index)
            e.DrawBackground()
            If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
                brText = Brushes.White
            ElseIf objDT.NH = "**" Or objDT.KH.Trim.Length = 3 Then
                brText = Brushes.Blue
            Else
                brText = Brushes.Black
            End If
            e.Graphics.DrawString(objDT.ToString(), e.Font, brText, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        Catch ex As Exception
            ShowMessages(ex.ToString())
        End Try
    End Sub
End Class